#!/bin/bash

# specify the arm as abi, the api level for android kitkat as used by
# google tango and with gnustl_static the c++ support
# for more information look into the android.toolchain.cmake file
export ANDROID_ABI="armeabi armeabi-v7a with NEON"
export ANDROID_NATIVE_API_LEVEL=android-19
export ANDROID_STL=gnustl_static
export ANDROID_STL_FORCE_FEATURES=ON
export ANDROID_NDK=/usr/local/android-ndk-r14b

export CFLAGS="-pipe -w"
export CXXFLAGS=${CFLAGS}

# [[ -z "${jobs}" ]] && jobs=$(grep -cP '^processor' /proc/cpuinfo)
jobs=4

echo -e "\n\n\033[1;32mCompiling with ${jobs} jobs ...\033[m"

# check that ANDROID_NDK points to a android ndk folder
# if [[ ! -d ${ANDROID_NDK} ]]
# then
#   echo -e "\e[1;31m[ERROR] ANDROID_NDK is not found! Set it as follows: export ANDROID_NDK=PATH_TO_YOUR_LOCAL_ANDROID_NDK_FOLDER\033[m"
#   exit 1
# fi

cd build

ROOT=${PWD}

set_android_path()
{
  ANDROIDTOOLCHAIN=${ROOT}/../android.toolchain.cmake

  EIGEN_INCLUDE_DIR=${ROOT}/eigen # eigen needs no cross-compiling
  FLANN_ROOT=${ROOT}/flann
  BOOST_ROOT=${ROOT}/boost
  PCL_ROOT=${ROOT}/pcl

  FLANN_LIBRARY=${ROOT}/flann-android/lib
  FLANN_INCLUDE_DIR=${ROOT}/flann-android/include

  export BOOST_ROOT=${ROOT}/boost-android
  Boost_LIBRARIES=${ROOT}/boost-android/lib
  Boost_INCLUDE_DIRS=${ROOT}/boost-android/include

  QHULL_LIBRARY=${ROOT}/qhull-android/lib
  QHULL_INCLUDE_DIR=${ROOT}/qhull-android/include
}

set_host_path()
{
  EIGEN_INCLUDE_DIR=${ROOT}/eigen # eigen needs no cross-compiling
  FLANN_ROOT=${ROOT}/flann
  BOOST_ROOT=${ROOT}/boost
  PCL_ROOT=${ROOT}/pcl
  QHULL_ROOT=${ROOT}/qhull

  FLANN_LIBRARY=${ROOT}/flann-host/lib
  FLANN_INCLUDE_DIR=${ROOT}/flann-host/include

  export BOOST_ROOT=${ROOT}/boost-host
  Boost_LIBRARIES=${ROOT}/boost-host/lib
  Boost_INCLUDE_DIRS=${ROOT}/boost-host/include

  QHULL_LIBRARY=${ROOT}/qhull-host/lib
  QHULL_INCLUDE_DIR=${ROOT}/qhull-host/include
}



function cmake_pcl_android {
  cmake . -DCMAKE_BUILD_TYPE:STRING=Release \
    -DCMAKE_INSTALL_PREFIX=${ROOT}/pcl-android \
    -DCMAKE_TOOLCHAIN_FILE:FILEPATH=$ANDROIDTOOLCHAIN \
    -DBUILD_SHARED_LIBS:BOOL=OFF \
    -DPCL_SHARED_LIBS:BOOL=OFF \
    -DBUILD_visualization:BOOL=OFF \
    -DBUILD_examples:BOOL=OFF \
    -DBUILD_tools:BOOL=OFF \
    -DEIGEN_INCLUDE_DIR:PATH=${EIGEN_INCLUDE_DIR} \
    -DFLANN_INCLUDE_DIR:PATH=${FLANN_INCLUDE_DIR} \
    -DFLANN_LIBRARY:FILEPATH=${FLANN_LIBRARY}/libflann_cpp_s.a \
    -DBOOST_ROOT:PATH=${BOOST_ROOT} \
    -DBoost_INCLUDE_DIR:PATH=${Boost_INCLUDE_DIRS} \
    -DWITH_VTK:BOOL=OFF \
    -DWITH_QT:BOOL=OFF \
    -DWITH_QHULL:BOOL=ON \
    -DQHULL_ROOT:PATH=${QHULL_ROOT} \
    -DQHULL_LIBRARY:FILEPATH=${QHULL_LIBRARY}/libqhullstatic.a \
    -DQHULL_INCLUDE_DIR:PATH=${QHULL_INCLUDE_DIR} \
    -DHAVE_QHULL_2011=ON \
    -DWITH_PCAP:BOOL=OFF \
    -DWITH_PNG:BOOL=OFF \
    -DWITH_OPENGL:BOOL=OFF \
    -DWITH_LIBUSB:BOOL=OFF \
    -DBoost_DATE_TIME_LIBRARY=${Boost_LIBRARIES}/libboost_date_time.a \
    -DBoost_DATE_TIME_LIBRARY_DEBUG=${Boost_LIBRARIES}/libboost_date_time.a \
    -DBoost_DATE_TIME_LIBRARY_RELEASE=${Boost_LIBRARIES}/libboost_date_time.a \
    -DBoost_FILESYSTEM_LIBRARY=${Boost_LIBRARIES}/libboost_filesystem.a \
    -DBoost_FILESYSTEM_LIBRARY_DEBUG=${Boost_LIBRARIES}/libboost_filesystem.a \
    -DBoost_FILESYSTEM_LIBRARY_RELEASE=${Boost_LIBRARIES}/libboost_filesystem.a \
    -DBoost_IOSTREAMS_LIBRARY=${Boost_LIBRARIES}/libboost_iostreams.a \
    -DBoost_IOSTREAMS_LIBRARY_DEBUG=${Boost_LIBRARIES}/libboost_iostreams.a \
    -DBoost_IOSTREAMS_LIBRARY_RELEASE=${Boost_LIBRARIES}/libboost_iostreams.a \
    -DBoost_SYSTEM_LIBRARY=${Boost_LIBRARIES}/libboost_system.a \
    -DBoost_SYSTEM_LIBRARY_DEBUG=${Boost_LIBRARIES}/libboost_system.a \
    -DBoost_SYSTEM_LIBRARY_RELEASE=${Boost_LIBRARIES}/libboost_system.a \
    -DBoost_THREAD_LIBRARY=${Boost_LIBRARIES}/libboost_thread.a \
    -DBoost_THREAD_LIBRARY_DEBUGBoost_INCLUDE_DIRS=${Boost_LIBRARIES}/libboost_thread.a \
    -DBoost_THREAD_LIBRARY_RELEASE=${Boost_LIBRARIES}/libboost_thread.a \
    -DBoost_LIBRARY_DIRS=${Boost_LIBRARIES}
}

function cmake_pcl_host {
  cmake . -DCMAKE_BUILD_TYPE:STRING=Release \
    -DCMAKE_INSTALL_PREFIX=${ROOT}/pcl-host \
    -DBUILD_SHARED_LIBS:BOOL=OFF \
    -DPCL_SHARED_LIBS:BOOL=OFF \
    -DBUILD_visualization:BOOL=OFF \
    -DBUILD_examples:BOOL=OFF \
    -DBUILD_tools:BOOL=OFF \
    -DEIGEN_INCLUDE_DIR:PATH=${EIGEN_INCLUDE_DIR} \
    -DFLANN_INCLUDE_DIR:PATH=${FLANN_INCLUDE_DIR} \
    -DFLANN_LIBRARY:FILEPATH=${FLANN_LIBRARY}/libflann_cpp_s.a \
    -DBOOST_ROOT:PATH=${BOOST_ROOT} \
    -DBoost_INCLUDE_DIR:PATH=${Boost_INCLUDE_DIRS} \
    -DWITH_VTK:BOOL=OFF \
    -DWITH_QHULL:BOOL=ON \
    -DQHULL_ROOT:PATH=${QHULL_ROOT} \
    -DQHULL_LIBRARY:FILEPATH=${QHULL_LIBRARY}/libqhullstatic.a \
    -DQHULL_INCLUDE_DIR:PATH=${QHULL_INCLUDE_DIR} \
    -DHAVE_QHULL_2011=ON \
    -DWITH_PCAP:BOOL=OFF \
    -DWITH_PNG:BOOL=OFF \
    -DWITH_OPENGL:BOOL=OFF \
    -DWITH_LIBUSB:BOOL=OFF \
    -DBoost_DATE_TIME_LIBRARY=${Boost_LIBRARIES}/libboost_date_time.a \
    -DBoost_DATE_TIME_LIBRARY_DEBUG=${Boost_LIBRARIES}/libboost_date_time.a \
    -DBoost_DATE_TIME_LIBRARY_RELEASE=${Boost_LIBRARIES}/libboost_date_time.a \
    -DBoost_FILESYSTEM_LIBRARY=${Boost_LIBRARIES}/libboost_filesystem.a \
    -DBoost_FILESYSTEM_LIBRARY_DEBUG=${Boost_LIBRARIES}/libboost_filesystem.a \
    -DBoost_FILESYSTEM_LIBRARY_RELEASE=${Boost_LIBRARIES}/libboost_filesystem.a \
    -DBoost_IOSTREAMS_LIBRARY=${Boost_LIBRARIES}/libboost_iostreams.a \
    -DBoost_IOSTREAMS_LIBRARY_DEBUG=${Boost_LIBRARIES}/libboost_iostreams.a \
    -DBoost_IOSTREAMS_LIBRARY_RELEASE=${Boost_LIBRARIES}/libboost_iostreams.a \
    -DBoost_SYSTEM_LIBRARY=${Boost_LIBRARIES}/libboost_system.a \
    -DBoost_SYSTEM_LIBRARY_DEBUG=${Boost_LIBRARIES}/libboost_system.a \
    -DBoost_SYSTEM_LIBRARY_RELEASE=${Boost_LIBRARIES}/libboost_system.a \
    -DBoost_THREAD_LIBRARY=${Boost_LIBRARIES}/libboost_thread.a \
    -DBoost_THREAD_LIBRARY_DEBUGBoost_INCLUDE_DIRS=${Boost_LIBRARIES}/libboost_thread.a \
    -DBoost_THREAD_LIBRARY_RELEASE=${Boost_LIBRARIES}/libboost_thread.a \
    -DBoost_LIBRARY_DIRS=${Boost_LIBRARIES}
}

build_host()
{
  echo -e "\n\n\033[1;35mm###########################################"
  echo -e "### PCL cross-compiling start...        ###"
  echo -e "###########################################\033[m\n\n"
  
  set_host_path

  cd ${PCL_ROOT}

  [ -f Makefile ] && make clean

  rm -rf ${ROOT}/pcl/CMakeCache.txt ${ROOT}/pcl/CMakeFiles

  rm -rf ${ROOT}/pcl-host
  mkdir ${ROOT}/pcl-host

  ### pcl cmake files do not set library paths properly. Therefore cmake has to be run twice.
  # cmake_pcl
  cmake_pcl_host
  # cmake_pcl_host

  echo -e "\n\n\033[1;32m make -j$jobs\033[m\n"
  echo -e "\n\n\033[1;32m this will run for a while... time to drink a\n"
  echo -e "   ( ( "
  echo -e "    ) ) "
  echo -e "  ........ "
  echo -e "  |      |] "
  echo -e "  \      /  "
  echo -e "   '----' \033[m\n\n"

  make -j${jobs}


  echo -e "\n\n\033[1;35m make install\033[m\n\n"
  make install

  cd ..
  echo "PCL cross-compiling finished!"

}

build_android()
{
  echo -e "\n\n\033[1;35mm###########################################"
  echo -e "### PCL cross-compiling start...        ###"
  echo -e "###########################################\033[m\n\n"
  
  set_android_path

  cd ${PCL_ROOT}

  [ -f Makefile ] && make clean

  rm -rf ${ROOT}/pcl/CMakeCache.txt ${ROOT}/pcl/CMakeFiles

  rm -rf ${ROOT}/pcl-android
  mkdir ${ROOT}/pcl-android

  ### pcl cmake files do not set library paths properly. Therefore cmake has to be run twice.
  # cmake_pcl
  cmake_pcl_android
  cmake_pcl_android

  echo -e "\n\n\033[1;32m make -j$jobs\033[m\n"
  echo -e "\n\n\033[1;32m this will run for a while... time to drink a\n"
  echo -e "   ( ( "
  echo -e "    ) ) "
  echo -e "  ........ "
  echo -e "  |      |] "
  echo -e "  \      /  "
  echo -e "   '----' \033[m\n\n"

   make -j${jobs}


  echo -e "\n\n\033[1;35m make install\033[m\n\n"
   make install

  cd ..
  echo "PCL cross-compiling finished!"

}

build_android 2>&1 | tee tmp_pcl.log
